#!/bin/sh

# 切换为master脚本
# author：huzhijian

#-----------------------------------获取当前脚本路径相关信息---------------------------------------------------
# 判断当前脚本是否为绝对路径，匹配以/开头下的所有
if [ $0 != ^\/.* ]
then
    script=$0
else
    script=$(pwd)/$0
fi

# 获取文件的真实路径
script=`readlink -f $script`
# 获取文件所在的目录
script_path=${script%/*}
# 获取文件所在目录的真实路径
real_path=$(readlink -f $script_path)
# 日志完整路径
log_path=$real_path/log/switch.log

mkdir $real_path/log
#---------------------------------------------------------------------------------------------------------------

profile_path=/etc/pep_mgmt_profile
if [ ! -f "$profile_path" ]; then
    echo "" | sudo tee -a $profile_path > /dev/null
fi

source $profile_path

date +"%Y-%m-%d %H:%M:%S begin to start" >> $log_path

start_timestamp=$(date +%s%3N)
temp_str=`cat $profile_path | grep START_TIMESTAMP`
if [ "$temp_str"x = ""x ]; then
    # 没有变量，追加
    echo "START_TIMESTAMP=$start_timestamp" | sudo tee -a $profile_path > /dev/null
else
    # 替换为最新启动时间
    old_start_timestamp=${START_TIMESTAMP}
    sudo sed -i 's|START_TIMESTAMP='"$old_start_timestamp"'|START_TIMESTAMP='"$start_timestamp"'|g' $profile_path
fi

sudo systemctl start pep_mgmt

date +"%Y-%m-%d %H:%M:%S run start finished" >> $log_path

exit 0
